using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Colorprj
{
    public partial class Test1 : Form
    {
        public Test1()
        {
            InitializeComponent();
        }
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

	BindingSource bindingsource1 = new BindingSource();
	BindingSource bindingsource2 = new BindingSource();
        private void Test1_Load(System.Object sender, System.EventArgs e)
	{

		DataTable main = new DataTable("main");
		main.Columns.Add("T1ID");
		main.Columns["T1ID"].Unique = true;
		main.Columns["T1ID"].AutoIncrement = true;
		main.Columns["T1ID"].AutoIncrementSeed = 1;


		main.Columns.Add("catID");
		main.Columns.Add("itemID");

		main.Rows.Add("1", "1", "4");
		main.Rows.Add("2", "2", "5");
		main.Rows.Add("3", "3", "10");


		DataTable catlookup = new DataTable("catlookup");
		catlookup.Columns.Add("catID");
		catlookup.Columns.Add("catname");

		catlookup.Rows.Add("1", "Dairy");
		catlookup.Rows.Add("2", "Meat");
		catlookup.Rows.Add("3", "Grains");


		DataTable itemlookup = new DataTable("itemlookup");

		itemlookup.Columns.Add("itemID");
		itemlookup.Columns.Add("catID");
		itemlookup.Columns.Add("itemname");

		itemlookup.Rows.Add("1", "1", "Milk");
		itemlookup.Rows.Add("2", "1", "Cheese");
		itemlookup.Rows.Add("3", "1", "Butter");
		itemlookup.Rows.Add("4", "1", "Cream");
		itemlookup.Rows.Add("5", "2", "Beef");
		itemlookup.Rows.Add("6", "2", "Pork");
		itemlookup.Rows.Add("7", "2", "Chicken");
		itemlookup.Rows.Add("8", "2", "Fish");
		itemlookup.Rows.Add("9", "2", "Mystery");
		itemlookup.Rows.Add("10", "3", "Buns");
		itemlookup.Rows.Add("11", "3", "Bread");
		itemlookup.Rows.Add("12", "3", "Cornmeal");
		itemlookup.Rows.Add("13", "3", "Tortilla");
		itemlookup.Rows.Add("14", "3", "Biscuits");








		DataSet dataset = new DataSet();

		dataset.Tables.Add(main);


		DataGridView1.DataSource = dataset;
		DataGridView1.DataMember = "main";



		DataSet lookup = new DataSet();

		lookup.Tables.Add(catlookup);
		lookup.Tables.Add(itemlookup);
		lookup.Relations.Add("lookup_relation", catlookup.Columns["catID"], itemlookup.Columns["catID"]);






		bindingsource1.DataSource = lookup;
		bindingsource1.DataMember = "catlookup";
		bindingsource2.DataSource = bindingsource1;
		bindingsource2.DataMember = "lookup_relation";



		DataGridView1.Columns.Remove("catID");

		DataGridViewComboBoxColumn comboboxColumn = new DataGridViewComboBoxColumn();
		comboboxColumn.DataPropertyName = "catID";
		comboboxColumn.HeaderText = "Category";
		comboboxColumn.DropDownWidth = 200;
		comboboxColumn.Width = 200;
		comboboxColumn.MaxDropDownItems = 5;
		comboboxColumn.FlatStyle = FlatStyle.Flat;

		comboboxColumn.DataSource = bindingsource1;
		comboboxColumn.ValueMember = "catID";
		comboboxColumn.DisplayMember = "catname";


		DataGridView1.Columns.Insert(1, comboboxColumn);

		DataGridView1.Columns.Remove("itemID");

		DataGridViewComboBoxColumn comboboxColumn2 = new DataGridViewComboBoxColumn();
		comboboxColumn2.DataPropertyName = "itemID";
		comboboxColumn2.HeaderText = "Item";
		comboboxColumn2.DropDownWidth = 200;
		comboboxColumn2.Width = 200;
		comboboxColumn2.MaxDropDownItems = 5;
		comboboxColumn2.FlatStyle = FlatStyle.Flat;

		comboboxColumn2.DataSource = lookup.Tables["itemlookup"];
		comboboxColumn2.ValueMember = "itemID";
		comboboxColumn2.DisplayMember = "itemname";


		DataGridView1.Columns.Insert(2, comboboxColumn2);





	}

        private void DataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (DataGridView1.CurrentCell.ColumnIndex == 2)
            {
                ComboBox control = (DataGridViewComboBoxEditingControl)e.Control;

                control.DataSource = bindingsource2;
                //DataGridView1.EndEdit();
            }
        }

        private void DataGridView1_CellLeave(object sender, DataGridViewCellEventArgs e)
        {
            
            if (DataGridView1.CurrentCell.ColumnIndex == 1)
            {
                //ComboBox control = (DataGridViewComboBoxEditingControl)e.Control;
               
               // comboboxColumn2.DataSource = bindingsource2;
                //DataGridView1.EndEdit();
            }
        }



    //private void DataGridView1_EditingControlShowing(object sender, System.Windows.Forms.DataGridViewEditingControlShowingEventArgs e)
    //{
    //    if (DataGridView1.CurrentCell.ColumnIndex == 2) {
    //        ComboBox control = (DataGridViewComboBoxEditingControl)e.Control;

    //        control.DataSource = bindingsource2;
    //        DataGridView1.EndEdit();
    //    }
    //}




        //private void Test1_Load(object sender, EventArgs e)
        //{
        //}
            //DataTable main = new DataTable("main");
            //main.Columns.Add("T1ID");
            //main.Columns["T1ID"].Unique = true;
            //main.Columns["T1ID"].AutoIncrement = true;
            //main.Columns["T1ID"].AutoIncrementSeed = 1;



            //main.Columns.Add("catID");
            //main.Columns.Add("itemID");

            //main.Rows.Add("1", "1", "4");
            //main.Rows.Add("2", "2", "5");
            //main.Rows.Add("3", "3", "10");


            //DataTable catlookup = new DataTable("catlookup");
            //catlookup.Columns.Add("catID");
            //catlookup.Columns.Add("catname");

            //catlookup.Rows.Add("1", "Dairy");
            //catlookup.Rows.Add("2", "Meat");
            //catlookup.Rows.Add("3", "Grains");


            //DataTable itemlookup = new DataTable("itemlookup");

            //itemlookup.Columns.Add("itemID");
            //itemlookup.Columns.Add("catID");
            //itemlookup.Columns.Add("itemname");

            //itemlookup.Rows.Add("1", "1", "Milk");
            //itemlookup.Rows.Add("2", "1", "Cheese");
            //itemlookup.Rows.Add("3", "1", "Butter");
            //itemlookup.Rows.Add("4", "1", "Cream");
            //itemlookup.Rows.Add("5", "2", "Beef");
            //itemlookup.Rows.Add("6", "2", "Pork");
            //itemlookup.Rows.Add("7", "2", "Chicken");
            //itemlookup.Rows.Add("8", "2", "Fish");
            //itemlookup.Rows.Add("9", "2", "Mystery");
            //itemlookup.Rows.Add("10", "3", "Buns");
            //itemlookup.Rows.Add("11", "3", "Bread");
            //itemlookup.Rows.Add("12", "3", "Cornmeal");
            //itemlookup.Rows.Add("13", "3", "Tortilla");
            //itemlookup.Rows.Add("14", "3", "Biscuits");








            //DataSet dataset = new DataSet();

            //dataset.Tables.Add(main);


            //DataGridView1.DataSource = dataset;
            //DataGridView1.DataMember = "main";



            //DataSet lookup = new DataSet();

            //lookup.Tables.Add(catlookup);
            //lookup.Tables.Add(itemlookup);
            //lookup.Relations.Add("lookup_relation", catlookup.Columns["catID"], itemlookup.Columns["catID"]);


            //BindingSource bindingsource1 = new BindingSource();
            //BindingSource bindingsource2 = new BindingSource();



            //bindingsource1.DataSource = lookup;
            //bindingsource1.DataMember = "catlookup";
            //bindingsource2.DataSource = bindingsource1;
            //bindingsource2.DataMember = "lookup_relation";



            //DataGridView1.Columns.Remove("catID");

            //DataGridViewComboBoxColumn comboboxColumn = new DataGridViewComboBoxColumn();
            //comboboxColumn.DataPropertyName = "catID";
            //comboboxColumn.HeaderText = "Category";
            //comboboxColumn.DropDownWidth = 200;
            //comboboxColumn.Width = 200;
            //comboboxColumn.MaxDropDownItems = 5;
            //comboboxColumn.FlatStyle = FlatStyle.Flat;

            //comboboxColumn.DataSource = bindingsource1;
            //comboboxColumn.ValueMember = "catID";
            //comboboxColumn.DisplayMember = "catname";


            //DataGridView1.Columns.Insert(1, comboboxColumn);

            //DataGridView1.Columns.Remove("itemID");

            //DataGridViewComboBoxColumn comboboxColumn2 = new DataGridViewComboBoxColumn();
            //comboboxColumn2.DataPropertyName = "itemID";
            //comboboxColumn2.HeaderText = "Item";
            //comboboxColumn2.DropDownWidth = 200;
            //comboboxColumn2.Width = 200;
            //comboboxColumn2.MaxDropDownItems = 5;
            //comboboxColumn2.FlatStyle = FlatStyle.Flat;

            //comboboxColumn2.DataSource = bindingsource2;
            //comboboxColumn2.ValueMember = "itemID";
            //comboboxColumn2.DisplayMember = "itemname";


            //DataGridView1.Columns.Insert(2, comboboxColumn2);
        //}
    }
}